<template>
  <section>
    <div class="top-box">
      <div class="top-item"><img src="../../assets/images/dangqianzongyonghushu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.totalCount}}</span><br>
            <span class="fontText">当前总用户人数</span>
          </p>
        </div>
      </div>
      <div class="top-item"><img src="../../assets/images/jinrixinzengyonghu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.todayAdd}}</span><br>
            <span class="fontText">今日新增用户(个)</span>
          </p>
        </div>
      </div>
      <div class="top-item"><img src="../../assets/images/jinyigeyuexinzengyonghu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.monthAdd}}</span><br>
            <span class="fontText">近一个月新增用户</span>
          </p>
        </div>
      </div>
      <div class="top-item"><img src="../../assets/images/jinnianxinzengyonghu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.yearAdd}}</span><br>
            <span class="fontText">今年新增用户</span>
          </p>
          <el-date-picker v-model="yearData"
                          @change="chageYearData"
                          type="year"
                          format="yyyy"
                          value-format="yyyy"
                          placeholder="选择年">
          </el-date-picker>
        </div>
      </div>
    </div>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      
      <el-form-item label="搜索名称"
                    prop="name">
        <el-input v-model="searchForm.info"
                  placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.sid"
                   placeholder="请选择状态">
          <el-option label="启用"
                     value="1"></el-option>
          <el-option label="禁用"
                     value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <btns @exec="exec"
          class="marbt20px"></btns>
    <div class="data-container scroll-table">
      <!-- 按钮列表 -->
      <el-table :data="tableData"
                border
                style="width: 100%"
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="40">
        </el-table-column>
        <el-table-column type="index"
                         label="序号"
                         width="50">
        </el-table-column>
        <el-table-column prop="nickname"
                         label="用户名称">
        </el-table-column>
        <el-table-column prop="phoneNum"
                         label="手机号码">
        </el-table-column>
        <el-table-column prop="rbc"
                         label="总回收站个数">
        </el-table-column>
        <el-table-column prop="createTime"
                         label="注册时间">
        </el-table-column>
        <el-table-column prop="state"
                         label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.state==1">启用</div>
            <div v-if="scope.row.state==2">禁用</div>
          </template>
        </el-table-column>
        <el-table-column prop="state"
                         label="操作">
          <template slot-scope="scope">
            <el-button @click="showDetails(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="mgt-10 pd-0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="searchForm.current"
                     :page-sizes="[10, 20, 30, 50]"
                     :page-size="searchForm.size"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="searchForm.totalCount">
      </el-pagination>
    </div>
    <!--新增修改界面-->

  </section>
</template>

<script>

import btns from '@/components/btns.vue'
import { format } from "../../utils/tool"
export default {
  components: {
    btns
  },
  data () {
    return {
      addForm: {},
      yearData: "",
      topData: {},
      btnForm: {},
      searchForm: {
        current: 1,
        size: 10,
        info: "",
        sid: "",
      },
      addORUpdateVisible: false,
      funTit: '',
      cityForm: {
        cityName: "",
        cityId: ''
      },
      rules: {
        "btnIcon": [],
        "btnName": [
          { required: true, message: '请输入城市名称', trigger: 'blur' }
        ],
        "btnCode": [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ],
        "sort": [
          { required: true, message: '排序', trigger: 'blur' }
        ]
      },
      tableData: [],
      tableSelection: []
    }
  },
  created () {
    // this.btnlist();
    this.getYearData();
    this.getTableData(1, 10, this.searchForm.info, this.searchForm.sid);
  },
  methods: {
    showDetails (row) {
      this.$router.push({
        path: "/user/userDetails",
        query: {
          userId: row.wxUserId
        }
      })
    },
    handleSizeChange (v) {
      console.log(v)
      this.searchForm.size = v;
      this.getTableData(this.searchForm.current, v, this.searchForm.info, this.searchForm.sid);
    },
    handleCurrentChange (v) {
      this.searchForm.current = v;
      this.getTableData(v, this.searchForm.size, this.searchForm.info, this.searchForm.sid);
    },
    getTableData (curr, size, info, state) {
      this.$api.user.getAdminUserList({
        current: curr,
        size: size,
        keyword: info,
        state: state,
      }).then(res => {
        if (res.code == 200) {
          this.tableData = res.data;
          this.searchForm.totalCount = res.totalCount;
          this.searchForm.size = res.pageSize
        }
      })
    },
    chageYearData () {
      this.$api.user.getAdminUserCount({
        year: this.yearData
      }).then(res => {
        if (res.code == 200) {
          this.topData = res.data
        }
      })
    },
    getYearData () {
      this.$api.user.getAdminUserCount().then(res => {
        if (res.code == 200) {
          this.topData = res.data;
        }
      })
    },
    handleReset () {
      // this.$refs.searchForm.resetFields();
      this.searchForm={
        info:'',
        sid:'',

      }
      this.getTableData(this.searchForm.current, this.searchForm.size, "", "");
    },
    search () {
      console.log(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid)
      this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid);
    },
    btnlist () {
      //获取接口
      this.$api.city.getList({}).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {

      this.btnForm = {
        "btnIcon": "",
        "btnName": "",
        "btnCode": "",
        "sort": 0
      }
      this.addORUpdateVisible = true;
    },
    update () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      this.btnForm = this.tableSelection[0];
      this.addORUpdateVisible = true;
    },
    del () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }

    },
    refresh () {
      this.btnlist();
    },
    enable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.state == 1) {
        this.$message.error('该数据已启用');
        return
      }
      this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.user.updateStatus({ userId: demo.wxUserId, status: 1 }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid);
            this.$message.success('启用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    disable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      console.log(demo)
      if (demo.state == 2) {
        this.$message.error('该数据已禁用');
        return
      }
      this.$confirm('此操作将禁用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.user.updateStatus({ userId: demo.wxUserId, status: 2 }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid);
            this.$message.success('禁用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.tableData.findIndex(c => c.btnCode == this.btnForm.btnCode) >= 0) {
            this.$message.error('code不能重复');
            return
          }
          if (this.funTit == 'add') {
            this.$api.system.btnadd(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            this.$api.system.btnupdate(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}

</script>
<style lang="less" scoped>
.marbt20px {
  margin-bottom: 20px;
}
.fontText {
  font-size: 20px;
}
.fontTop {
  font-size: 40px;
}
.top-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}
.top-item {
  border: 1px gray solid;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 20%;
  padding: 20px;
  .top-icon {
    width: 35%;
    height: 35%;

  }
  .right-item-box {
    text-align: center;
    margin-left: 20px;
  }
}
</style>